//
//  StatusCellTopView.swift
//  SinaWeiBo20
//
//  Created by tarena on 16/3/25.
//  Copyright © 2016年 tarena. All rights reserved.
//

import UIKit
import SnapKit

class StatusCellTopView: UIView {
    
    //定义 视图的底部约束的属性
    var bottomConstraints: Constraint?
    
    //定义微博模型属性
    var status: Status? {
        didSet{
            //绑定数据
            iconImageView.sd_setImageWithURL(status?.user?.profile_url)
            nameLabel.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrank_image
            
            //TODO: 后期完善
            timeLabel.text = NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
            sourceLabel.text = status?.sourceText
            
            //表情：
            contentLabel.text = status?.text
            
            //给配图视图的数组赋值
            pictureView.imageURLs = status?.imageURLs
            
//            print("111\(pictureView.imageURLs)")
            
            //根据数据中是否有配图视图 然后动态调整约束关系  涉及到cell的复用 约束对象也会被复用
            //status?.imageURLs 永远都不为空 如果没有配图视图会给定一个空数组
            //where : 可以获取到前面快速赋值不为空的对象
            //如果imageURLs 不为空 并且 imageURLs.count 不为 0
            
            
            //解决约束冲突
            //1.先卸载老的约束对象 
            self.bottomConstraints?.uninstall()
            
            //2. 根据条件重新添加约束对象
            if let imageURLs = status?.imageURLs where imageURLs.count != 0 {
                //有配图视图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                })
                
            }else {
                //没有配图视图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMarigin).constraint
                })
            }
        }
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //背景色  －－－调试用
        backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //自定义顶部视图
    private func setupUI() {
        //添加子视图
        addSubview(sepView)
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(verified_type_image)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        sepView.backgroundColor = UIColor.randomColor()
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.height.equalTo(10)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMarigin)
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMarigin)
            make.size.equalTo(CGSize(width: StatusCellHeadImageWidth, height: StatusCellHeadImageWidth))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_top)
            make.left.equalTo(iconImageView.snp_right).offset(StatusCellMarigin)
        }
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMarigin)
        }
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_right)
            make.centerY.equalTo(iconImageView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.left.equalTo(iconImageView.snp_right).offset(StatusCellMarigin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(self.snp_left).offset(StatusCellMarigin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(contentLabel.snp_left)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
        
        
    }
    
    
    //加载所有的子视图
    private lazy var sepView: UIView = UIView()
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(title: "Shaw", fontSize: 14, color: themeColor)
    private lazy var mbrankImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verified_type_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var timeLabel: UILabel = UILabel(title: "08:00", fontSize: 10, color: themeColor)
    private lazy var sourceLabel: UILabel = UILabel(title: "邻家小妹", fontSize: 10, color: themeColor)
    private lazy var contentLabel: UILabel = UILabel(title: "下雨天炸鸡和辣条更加配哦", fontSize: 14, color: UIColor.darkGrayColor(), margin: StatusCellMarigin)
    
    //配图视图
    
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    

}
